var windowName = "shopInfo";
var Page,count = 0,ProductPics,HeadPics;
apiready = function(){

    var imageBrowser = api.require('imageBrowser')
    ProductPics = new PicUp()
    HeadPics = new PicUp();
    onWindowShow(function () {
        console.log(windowName + " show")
        if (count == 0){count++;return;}
        var data = getLocData("mapPickerData")
        logJson(data,"mapData: ")
        if (isTrue(data)) {
            Page.mapPickerData = data
            Page.mapPickerData.show = true
            Page.mapPickerDataShow = true
            // {"title":"众成明月华庭","uid":"40fd5e69ab91616aee74f80f","point":{"lng":118.657893,"lat":37.411411},"city":"东营市","Si":"房地产","type":0,"address":"山东省东营市东营区G220(南二路)","postcode":null,"phoneNumber":null,"lu":["房地产"]}
            // {"title":"众成明月华庭","uid":"40fd5e69ab91616aee74f80f","point":{"lng":118.657893,"lat":37.411411},"city":"东营市","Si":"房地产","type":0,"address":"山东省东营市东营区G220(南二路)","postcode":null,"phoneNumber":null,"lu":["房地产"]}
            // {"title":"众成明月华庭","uid":"40fd5e69ab91616aee74f80f","point":{"lng":118.657893,"lat":37.411411},"city":"东营市","Si":"房地产","type":0,"address":"山东省东营市东营区G220(南二路)","postcode":null,"phoneNumber":null,"lu":["房地产"]}
            // {"lon":118.675322,"lat":37.434984,"name":"众成商城","uid":"afc018bde930ec6438e4a64c","address":"168路","city":"东营市","phone":""}

            //使用这种格式
            // {"lon":118.638399,"lat":37.440736,"name":"众成禧园","uid":"1a7329e3be8fb3c44f6d4637","address":"东一路与辽河路交叉口东南150米","city":"东营市","phone":""}
            Page.shop.lat = data.lat
            Page.shop.lon = data.lon
            Page.shop.city = data.city
        }
    })
    onWindowHide(function () {
        console.log(windowName + " hide")
    })
    console.log("index page is OK");
    Page = createVM("#Page",{
        shop:{
            "name":"用户_15265461237的店铺",
            "logo":"",
            "head":"/ApiCloud/assets/img/banner.jpg",
            "openStart":0,
            "openEnd":0,
            "province":null,
            "city":null,
            "area":null,
            "address":"",
            "intro":"欢迎光临我的店铺！",
            "pics":[],
            "lat":null,
            "lon":null,
            "phone":"15265461237",
            "openStr":"全天营业"
        }
        ,title:""
        ,content:""
        ,pics:[]


        //地图相关
        ,mapPickerData:{}
        ,mapPickerDataShow:false
        //省市县去选择
        ,orgData:[]
        ,pro:{}
        ,proIndex:0
        ,cit:{}
        ,citIndex:0
        ,are:{}
        ,areIndex:0
        ,pcaStep:1
        ,pcaShow:false
    },{
        //页面方法
        "init":function () {
            var shop = getLocData("shopInfo")
            this.shop = shop
            this.orgData = citys;
            getData()
        }
        , setAddress: function (text) {
            if (Page.shop.address == null) {
                Page.shop.address = ""
            }
            Page.shop.address += text;
        }
        , hideFastInput: function () {
            logJson(Page.mapPickerData.show)
            Page.mapPickerDataShow = !Page.mapPickerDataShow
        }
        ,selectHeadPic:function () {
            var _this = this;
            HeadPics.uploadedPath = [];
            HeadPics.select(1).setCbFunc(function (pics) {
                logJson(pics,"pics_selectHeadPic")
                // _this.pics = ProductPics.getViewData()
                if (HeadPics.getData()) {
                    _this.shop.logo = HeadPics.getData()[0];
                }

            })
        },
        selectPic:function () {
            var _this = this;
            ProductPics.select(9).setCbFunc(function (pics) {
                logJson(pics,"pics")
                _this.pics = ProductPics.getViewData()
                _this.shop.pics = ProductPics.getData()
            })
        },
        del:function (index) {
            var pics = ProductPics.removePic(index)
            this.pics = ProductPics.getViewData()
            this.shop.pics = ProductPics.getData()
        },
        move:function (index,num) {
            var pics = ProductPics.picMove(index,num)
            this.pics = ProductPics.getViewData()
            this.shop.pics = ProductPics.getData()
        },
        showPic:function (index,pics) {
            imageBrowser.openImages({
                imageUrls: pics,
                activeIndex:index
            });
        }
        ,submit: function () {
            if (!isTrue(this.shop.name)){showToast("名称不能为空");return}
            if (!isTrue(this.shop.logo)){showToast("请设置LOGO");return}
            if (!isTrue(this.shop.address)){showToast("地址不能为空");return}
            if (!isTrue(this.shop.phone)){showToast("联系电话不能为空");return}
            if (!isTrue(this.shop.pics)){showToast("请选择至少一张企业相册图片");return}
            apiPost("/app/my/shop", this.shop, function (res ,code) {
                logJson(res)
                if (code == 200){
                    showToast(res.message)
                }else {
                    showToast("系统繁忙")
                }
            });
        }
        //地区选择器相关
        , pcaShowFunc:function () {
            this.pcaShow = !this.pcaShow
        }
        , setP: function (proIndex,pro) {
            this.pro = pro
            this.proIndex = proIndex
            this.pcaStep = 2
            logJson(this.pcaStep)
            logJson(pro)
        }
        , setC: function (citIndex,cit) {
            this.cit = cit
            this.citIndex = citIndex
            this.pcaStep = 3
        }
        , setA: function (areIndex,are) {
            this.are = are
            this.areIndex = areIndex
            this.pcaStep = 1
            //todo 设置选择完成后的逻辑，手动关闭不算
            this.shop.province = this.pro.name
            this.shop.city = this.cit.name
            this.shop.area = this.are.name
            this.code = this.are.id
            this.pcaShow = false;
        }
    })
    Page.init()
}

function getData() {
    apiGet("/app/my/shop/info",{},function (res,code) {
        logJson(res.data,"/app/my/shop/info")
        setLocData("shopInfo",res.data)
        Page.shop = res.data;
        ProductPics.uploadedPath = Page.shop.pics
        Page.pics = ProductPics.getViewData()
    })
}